package com.woniu108.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.carService.commos.dto.AdminInfoDto;
import com.woniu108.carService.commos.exception.admin.AdminException;
import com.woniu108.carService.commos.exception.admin.AdminExceptionCode;
import com.woniu108.carService.commos.exception.car.CarException;
import com.woniu108.carService.commos.exception.car.CarExceptionCode;
import org.springframework.stereotype.Service;
import com.woniu108.car.client.PersonClient;
import com.woniu108.car.mapper.CarSeriesYearMapper;
import com.woniu108.car.model.CarSeriesYear;
import com.woniu108.car.param.InsertCarSeriesYearParam;
import com.woniu108.car.service.CarSeriesYearService;

import javax.annotation.Resource;

/**
 * <p>
 * 车系生产年份表 服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class CarSeriesYearServiceImpl extends ServiceImpl<CarSeriesYearMapper, CarSeriesYear> implements CarSeriesYearService {
    @Resource
    private CarSeriesYearMapper carSeriesYearMapper;
    @Resource
    private PersonClient personClient;

    /**
     * 判断管理员
     */
    private void isAdmin(String adminId) {
        // 检查 adminId 是否为 null
        if (adminId == null) {
            throw new AdminException(AdminExceptionCode.ADMIN_ID_NULL);
        }
        // 判断管理员权限
        AdminInfoDto adminInfo = personClient.getByAdminId(adminId).getData();
        if (adminInfo.getAdminRoleId() != 1) {
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_PERMISSION);
        }
    }
    @Override
    public Boolean insertCarSeriesYear(InsertCarSeriesYearParam param) {
        isAdmin(param.getAdminId());
        QueryWrapper<CarSeriesYear> wrapper = new QueryWrapper<>();
        wrapper.eq("c_s_year",param.getCSYear());
        CarSeriesYear selectOne = carSeriesYearMapper.selectOne(wrapper);
        if (selectOne!=null){
            throw new CarException(CarExceptionCode.CAR_SERIES_YEAR_HAVE);
        }
        CarSeriesYear year = new CarSeriesYear();
        year.setCSYear(param.getCSYear());
        int insert = carSeriesYearMapper.insert(year);
        return insert>0;
    }
}
